#include "crecipemodel.h"

#include <QSqlRecord>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include "crecipemodel.h"
#include "cconstdefinition.h"

using namespace cooking;

const QString CRecipeModel::KRecipeRoleNamesMap[][2] = {
    {KRecipeId, KTableRecipe + KDot + KRecipeId},
//    {KRecipeCuisineId, KTableRecipe + KDot + KRecipeCuisineId},
    {KRecipeName, KTableRecipe + KDot + KRecipeName},
    {KRecipePicture, KTableRecipe + KDot + KRecipePicture},
    {KNullString, KNullString}
};

CRecipeModel::CRecipeModel(CEngine& engine, QObject *parent) :
    CModel(engine, parent), m_cuisineId(-1),
    m_roleStart(-1), m_roleEnd(-1),
    m_filter(NULL) {
    initRoles();
    refresh();
}

CRecipeModel::CRecipeModel(int cuisineId, CEngine& engine, QObject *parent) :
    CModel(engine, parent), m_cuisineId(cuisineId),
    m_roleStart(-1), m_roleEnd(-1), m_filter(NULL) {
    initRoles();
    refresh();
}
CRecipeModel::~CRecipeModel() {
}

int CRecipeModel::rowCount(const QModelIndex& parent) const {
    int count = QSqlQueryModel::rowCount(parent);
    qDebug()<<"rowCount:"<<count;
    return count;
}

QVariant CRecipeModel::data(const QModelIndex& index, int role) const {
    QVariant value;
    if (role >= m_roleStart && role <= m_roleEnd) {
        int column = role - m_roleStart;
        QModelIndex roleIndex = this->index(index.row(), column);
        value = QSqlQueryModel::data(roleIndex);
    }
    else {
        qDebug()<<"wrong role:"<<role;
    }

    return value;
}

int CRecipeModel::cuisineId() {
    return m_cuisineId;
}

void CRecipeModel::setCuisineId(int cuisineId) {
    if (m_cuisineId != cuisineId) {
        beginResetModel();
        m_cuisineId = cuisineId;
        setQuery(queryString());
        emit cuisineIdChanged();
        endResetModel();
    }
}

CRecipeFilter* CRecipeModel::filter() {
    return &m_filter;
}

void CRecipeModel::setFilter(CRecipeFilter* filter) {
    if (m_filter != *filter) {
        beginResetModel();
        m_filter = *filter;
        setQuery(queryString());
        emit filterChanged();
        endResetModel();
    }
}

void CRecipeModel::initRoles() {
    QHash<int, QByteArray> roles;
    int index = 0;

    while (KRecipeRoleNamesMap[index][0] != KNullString) {
        roles[index + KRecipeRoleStart] = KRecipeRoleNamesMap[index][0].toLocal8Bit();
        ++index;
    }
    m_roleStart = KRecipeRoleStart;
    m_roleEnd = index + KRecipeRoleStart - 1;
    setRoleNames(roles);
}

void CRecipeModel::refresh() {
    qDebug()<<"refresh";
    setQuery(queryString());
}

QString CRecipeModel::queryString() {
    QString queryString(KSelect);
    int i = 0;
    bool roleAdded = false;
    while (KRecipeRoleNamesMap[i][0] != KNullString) {
        // only add real columns
        if (KRecipeRoleNamesMap[i][1] != KNullString) {
            //add comma if role added and not the first one.
            if (i != 0 && roleAdded) {
                queryString += KComma;
            }
            queryString += KRecipeRoleNamesMap[i][1];
            roleAdded = true;
        }
        ++i;
    }

    QString ingredientFilter = sqlIngredients();
    QString recipeColumnFilter = sqlRecipe();
    QString cuisineFilter = sqlCuisine();
    QString readyTimeFilter = sqlReadyTime();
    QString nutritionFilter = sqlNutritions();
    queryString += KFrom+KTableRecipe;
    if (!ingredientFilter.isEmpty() || recipeColumnFilter.isEmpty()
            || !cuisineFilter.isEmpty() || !readyTimeFilter.isEmpty()
            || !nutritionFilter.isEmpty()) {
        queryString += KWhere+KRecipeId+KNot+KIn+"(";
        QString whereSql;
        sqlAddWhereUnion(whereSql, ingredientFilter);
        sqlAddWhereUnion(whereSql, recipeColumnFilter);
        sqlAddWhereUnion(whereSql, cuisineFilter);
        sqlAddWhereUnion(whereSql, readyTimeFilter);
        sqlAddWhereUnion(whereSql, nutritionFilter);
        queryString += whereSql+")";
    }


    qDebug()<<"query string:"<<queryString;
    return queryString;
}

QString CRecipeModel::sqlIngredients() {
    QString exist = sqlIngredientsExist();
    QString include = sqlIngredientsInclude();
    QString exclude = sqlIngredientsExclude();
    QString may = sqlIngredientsMay();
    QString sql;
    bool existInAdded = false;

    if (!exist.isEmpty()) {
        /*
          select recipeId from (exist)
          where recipeId in (
        */
        sql += KSelect + KRecipeIngredientRecipeId + KFrom + "("
                + exist + ")";
        if (!include.isEmpty() || !exclude.isEmpty() || !may.isEmpty()) {
            sql += KWhere + KRecipeIngredientRecipeId + KIn + "(";
            existInAdded = true;
        }
    }

    bool added(false);
    sqlAddIntersect(sql, added, include);
    sqlAddIntersect(sql, added, exclude);
    sqlAddIntersect(sql, added, may);

    if (existInAdded) {
        sql += ")";
    }
    return sql;
}

QString CRecipeModel::sqlIngredientsExist() {
    QString sql;
    const QString allCnt("allCnt");
    const QString aliasTab("re_ing");
    const QString allIngCnt("ing_cnt");
    const QString minus("minus");

    QList<int> list = m_filter.existIngredients();
    if (list.count()) {
        /*select recipeId from (
            select re_ing.recipeId,
                    count(re_ing.ingredientId),
                    allcnt.ing_cnt,
                    (allcnt.ing_cnt - count(re_ing.ingredientId)) as minus
            from recipe_ingredient as re_ing
                 join
                 (select recipeId, count(ingredientId) as ing_cnt
                  from recipe_ingredient
                  group by recipeId)allcnt
                 on re_ing.recipeId = allcnt.recipeId
            where re_ing.ingredientId in (1,3,4,5,6,7)
            group by re_ing.recipeId
            order by minus asc
            )
        */
        sql += KSelect + KRecipeIngredientRecipeId + KFrom + "("
                + KSelect + aliasTab + KDot + KRecipeIngredientRecipeId + KComma
                + KCount + "(" + aliasTab + KDot + KRecipeIngredientRecipeId + ")" + KComma
                + allCnt + KDot + allIngCnt + KComma
                + "(" + allCnt + KDot + allIngCnt + KMinus + KCount + "(" + aliasTab + KDot + KRecipeIngredientRecipeId + ")" + ")" + KAs + minus
                + KFrom + KTableRecipeIngredient + KAs + aliasTab
                + KJoin
                + "(" + KSelect + KRecipeIngredientRecipeId + KComma + KCount + "(" + KRecipeIngredientIngredientId +")" + KAs + allIngCnt
                + KFrom + KTableRecipeIngredient
                + KGroupBy + KRecipeIngredientRecipeId + ")" + allCnt
                + KOn + aliasTab+KDot+KRecipeIngredientRecipeId + KEqual + allCnt+KDot+KRecipeIngredientRecipeId
                + KWhere + aliasTab+KDot+KRecipeIngredientIngredientId + KIn + "(";
        sqlAddListNumber(sql, list);
        sql += ")"
                + KGroupBy + aliasTab+KDot+KRecipeIngredientRecipeId
                + KOrderBy + minus + KAsc
                +")";
    }
    return sql;
}

QString CRecipeModel::sqlIngredientsInclude() {
    QString sql;
    QList<int> list = m_filter.ingredients(CRecipeFilter::EInclude);
    if (list.count()) {
        const QString ingCnt("ing_cnt");
        /*
        select recipeId from (
            select recipeId,
                   count(ingredientId) as ing_cnt
            from recipe_ingredient
            where ingredientId in (2)
            group by recipeId
            having ing_cnt = 1
        )
        */
        sql += KSelect + KRecipeIngredientRecipeId + KFrom + "("
                + KSelect + KRecipeIngredientRecipeId + KComma
                + KCount + "(" +KRecipeIngredientIngredientId + ")" + KAs + ingCnt
                + KFrom + KTableRecipeIngredient
                + KWhere + KRecipeIngredientIngredientId + KIn + "(";
        sqlAddListNumber(sql, list);
        sql += ")"
                + KGroupBy + KRecipeIngredientRecipeId
                + KHaving + ingCnt + KEqual + QString::number(list.count())
                +")";
    }
    return sql;
}

QString CRecipeModel::sqlIngredientsExclude() {
    QString sql;
    QList<int> list = m_filter.ingredients(CRecipeFilter::EExclude);
    if (list.count()) {
        /*
        select distinct recipeId
        from recipe_ingredient
        where recipeId not in (
            select distinct recipeId
            from recipe_ingredient
            where ingredientId in (9))
        )
        */
        sql += KSelect + KDistinct + KRecipeIngredientRecipeId
                + KFrom + KTableRecipeIngredient
                + KWhere + KRecipeIngredientRecipeId + KNot +KIn + "("
                + KSelect + KDistinct + KRecipeIngredientRecipeId
                + KFrom + KTableRecipeIngredient
                + KWhere + KRecipeIngredientIngredientId + KIn + "(";

        sqlAddListNumber(sql, list);
        sql += ")";
        sql += ")";
    }
    return sql;
}

QString CRecipeModel::sqlIngredientsMay() {
    QString sql;
    QList<int> list = m_filter.ingredients(CRecipeFilter::EMay);
    if (list.count()) {
        /*
        select distinct recipeId
        from recipe_ingredient
        where ingredientId in (2, 3, 9)
        */
        sql += KSelect + KDistinct + KRecipeIngredientRecipeId
                + KFrom + KTableRecipeIngredient
                + KWhere + KRecipeIngredientRecipeId +KIn + "(";

        sqlAddListNumber(sql, list);
        sql += ")";
    }
    return sql;
}

QString CRecipeModel::sqlRecipe() {
    QString sql;
    bool filterAdded(false);
    //For following four attributes, include can only have one,
    //so if include is exist, other two fields do not need to be checked

    QList<int> techInlist = m_filter.techniques(CRecipeFilter::EInclude);
    QList<int> techExlist = m_filter.techniques(CRecipeFilter::EExclude);
    QList<int> techMaylist = m_filter.techniques(CRecipeFilter::EMay);
    sqlAddRecipeColumnFilter(sql, filterAdded, KRecipeTechniqueId,
                             techInlist, techExlist, techMaylist);

    QList<int> flavInlist = m_filter.flavours(CRecipeFilter::EInclude);
    QList<int> flavExlist = m_filter.flavours(CRecipeFilter::EExclude);
    QList<int> flavMaylist = m_filter.flavours(CRecipeFilter::EMay);
    sqlAddRecipeColumnFilter(sql, filterAdded, KRecipeFlavourId,
                             flavInlist, flavExlist, flavMaylist);

    QList<int> dishInlist = m_filter.dishTypes(CRecipeFilter::EInclude);
    QList<int> dishExlist = m_filter.dishTypes(CRecipeFilter::EExclude);
    QList<int> dishMaylist = m_filter.dishTypes(CRecipeFilter::EMay);
    sqlAddRecipeColumnFilter(sql, filterAdded, KRecipeDishTypeId,
                             dishInlist, dishExlist, dishMaylist);

    QList<int> foodInlist = m_filter.foodTypes(CRecipeFilter::EInclude);
    QList<int> foodExlist = m_filter.foodTypes(CRecipeFilter::EExclude);
    QList<int> foodMaylist = m_filter.foodTypes(CRecipeFilter::EMay);
    sqlAddRecipeColumnFilter(sql, filterAdded, KRecipeFoodTypeId,
                             foodInlist, foodExlist, foodMaylist);
    return sql;
}

void CRecipeModel::sqlAddListNumber(QString& sql, QList<int>& list) {
    for (int i = 0; i < list.count(); i++) {
        sql += QString::number(list[i]);
        if (i != list.count()) {
            sql += KComma;
        }
    }
}

void CRecipeModel::sqlAddAnd(QString& sql, bool filterAdded) {
    if (filterAdded) {
        sql += KAnd;
    }
}


void CRecipeModel::sqlAddRecipeColumnFilter(QString& sql,
                                            bool& filterAdded,
                                            QString column,
                                            QList<int>& inList,
                                            QList<int>& exList,
                                            QList<int>& mayList) {
    if (inList.count()) {
        sqlAddAnd(sql, filterAdded);
        sql += column + KEqual + inList[0];
        filterAdded = true;
    }
    else {
        if (exList.count()) {
            sqlAddAnd(sql, filterAdded);
            sql += column + KNot + KIn + "(";
            sqlAddListNumber(sql, exList);
            sql += ")";
            filterAdded = true;
        }

        if (mayList.count()) {
            sqlAddAnd(sql, filterAdded);
            sql += column + KIn + "(";
            sqlAddListNumber(sql, mayList);
            sql += ")";
            filterAdded = true;
        }
    }
}

QString CRecipeModel::sqlCuisine() {
    QString include = sqlCuisineInclude();
    QString exclude = sqlCuisineExclude();
    QString may = sqlCuisineMay();
    QString sql;
    bool added(false);

    sqlAddIntersect(sql, added, include);
    sqlAddIntersect(sql, added, exclude);
    sqlAddIntersect(sql, added, may);
    return sql;
}

QString CRecipeModel::sqlCuisineInclude() {
    QString sql;
    QList<int> list = m_filter.cuisines(CRecipeFilter::EInclude);
    if (list.count()) {
        /*
        select recipeId from (
            select recipeId,
                   count(cuisineId)
            from cuisine_recipe
            where cuisineId in (2, 3, 9)
            group by recipeId
            having count(cuisineId) = 3
        )
        */
        sql += KSelect + KCuisineRecipeRecipeId + KFrom + "("
                + KSelect + KCuisineRecipeRecipeId + KComma
                + KCount + "(" + KCuisineRecipeCuisineId + ")"
                + KFrom + KTableCuisineRecipe
                + KWhere + KCuisineRecipeCuisineId +KIn + "(";
        sqlAddListNumber(sql, list);
        sql += ")"
                + KGroupBy + KCuisineRecipeRecipeId
                + KHaving + KCount + "(" + KCuisineRecipeCuisineId + ")" + KEqual + QString::number(list.count())
                + ")";
    }
    return sql;
}

QString CRecipeModel::sqlCuisineExclude() {
    QString sql;
    QList<int> list = m_filter.cuisines(CRecipeFilter::EInclude);
    if (list.count()) {
        /*
        select distinct recipeId
        from cuisine_recipe
        where cuisineId not in (2, 3, 9)
        */
        sql += KSelect + KDistinct + KCuisineRecipeRecipeId
                + KFrom + KTableCuisineRecipe
                + KWhere + KCuisineRecipeCuisineId + KNot + KIn + "(";
        sqlAddListNumber(sql, list);
        sql += ")";
    }
    return sql;
}

QString CRecipeModel::sqlCuisineMay() {
    QString sql;
    QList<int> list = m_filter.cuisines(CRecipeFilter::EMay);
    if (list.count()) {
        /*
        select distinct recipeId
        from cuisine_recipe
        where cuisineId in (2, 3, 9)
        */
        sql += KSelect + KDistinct + KCuisineRecipeRecipeId
                + KFrom + KTableCuisineRecipe
                + KWhere + KCuisineRecipeCuisineId +KIn + "(";
        sqlAddListNumber(sql, list);
        sql += ")";
    }
    return sql;
}

void CRecipeModel::sqlAddIntersect(QString& sql, bool& added, const QString intersectSql) {
    if (!intersectSql.isEmpty()) {
        if (added) {
            sql += KIntersect;
        }
        sql += intersectSql;
        added = true;
    }
}

QString CRecipeModel::sqlReadyTime() {
    QString sql;
    int equal = m_filter.readyTimes(CRecipeFilter::EEqual);
    //If equal time is present, just need to find equal time
    if (equal != KInvalidId) {
        /**
          preparing_time + cooking_time = 5
         */
        sql += KRecipePreparingTime + KOpAdd + KRecipeCookingTime
                + KOpEqual + QString::number(equal);
        return sql;
    }

    int less = m_filter.readyTimes(CRecipeFilter::ELess);
    int lessEq = m_filter.readyTimes(CRecipeFilter::ELessOrEqual);
    int more = m_filter.readyTimes(CRecipeFilter::EMore);
    int moreEq = m_filter.readyTimes(CRecipeFilter::EMoreOrEqual);

    int least(KInvalidId);
    CRecipeFilter::EMoreLessType leastType;
    least = less;
    leastType = CRecipeFilter::ELess;
    if (lessEq != KInvalidId) {
        if (least != KInvalidId) {
            if (lessEq < least) {
                least = lessEq;
                leastType = CRecipeFilter::ELessOrEqual;
            }
        }
        else {
            least = lessEq;
            leastType = CRecipeFilter::ELessOrEqual;
        }
    }
    if (least != KInvalidId) {
        /**
          preparing_time + cooking_time <= 5
         */
        sql += KRecipePreparingTime + KOpAdd + KRecipeCookingTime;
        if (leastType == CRecipeFilter::ELessOrEqual) {
            sql += KOpLessEqual;
        }
        else if (leastType == CRecipeFilter::ELess) {
            sql += KOpLess;
        }
        sql += QString::number(least);
    }

    int most(KInvalidId);
    CRecipeFilter::EMoreLessType mostType;
    most = more;
    mostType = CRecipeFilter::EMore;
    if (moreEq != KInvalidId) {
        if (most != KInvalidId) {
            if (moreEq > most) {
                most = moreEq;
                mostType = CRecipeFilter::EMoreOrEqual;
            }
        }
        else {
            most = moreEq;
            mostType = CRecipeFilter::EMoreOrEqual;
        }
    }

    if (most != KInvalidId) {
        if (least != KInvalidId) {
            sql += KAnd;
        }
        sql += KRecipePreparingTime + KOpAdd + KRecipeCookingTime;

        if (mostType == CRecipeFilter::EMoreOrEqual) {
            sql += KOpMoreEqual;
        }
        else if (mostType == CRecipeFilter::EMore) {
            sql += KOpMore;
        }
        sql += QString::number(most);
    }

    return sql;
}

int CRecipeModel::sqlFindLeast(QList<int>& list) {
    int index(KInvalidId);
    for (int i = 0; i < list.count(); i++) {
        if (index == KInvalidId) {
            index = 0;
            continue;
        }

        if (list[i] < list[index]) {
            index = i;
        }
    }
    return index;
}

int CRecipeModel::sqlFindLargest(QList<int>& list) {
    int index(KInvalidId);
    for (int i = 0; i < list.count(); i++) {
        if (index == KInvalidId) {
            index = 0;
            continue;
        }

        if (list[i] > list[index]) {
            index = i;
        }
    }
    return index;
}

QString CRecipeModel::sqlNutritions() {
    QList<CNutritionFilter> filters = m_filter.nutritions();
    bool perServe = sqlNutritionsHasPerServe();
    QString sql;
    const QString recipeId("recipeId");
    const QString nutritionId("nutritionId");
    const QString nutritionSum("nutritionSum");
    const QString nutritionQ("nutritionQ");
    /*
    select id
    from recipe
    where id not in (
        select recipeId from (
            select recipeId,  nutritionId, sum(nutritionQ) as nutritionSum from (
                select recipe.id as recipeId, nutrition.id as nutritionId, 0 as nutritionQ
                from recipe join nutrition
                union
                select recipe_ingredient.recipeId, IngredientNutrition.nutritionId,
                        sum(IngredientNutrition.nutritionQuantity*
                            recipe_ingredient.ingredientQuantity/
                            IngredientNutrition.ingredientQuantity/recipe.serve)
                from recipe_ingredient join IngredientNutrition on
                     recipe_ingredient.ingredientId = IngredientNutrition.ingredientId
                     join recipe on recipe_ingredient.recipeId = recipe.id
                group by recipe_ingredient.recipeId, IngredientNutrition.nutritionId
                )
            group by recipeId, nutritionId
            having (nutritionId = 1 and nutritionSum > 7.45) or (nutritionId = 2 and nutritionSum >5)
        )
    )
    */
    sql += KSelect+KRecipeId
            +KFrom+KTableRecipe
            +KWhere+KRecipeId+KNot+KIn+"("
            +KSelect+recipeId+KComma+nutritionId+KComma+KSum+"("+nutritionQ+")"+KAs+nutritionSum+KFrom+"("
            +KSelect+KTableRecipe+KDot+KRecipeId+KAs+recipeId+KComma+KTableNutrition+KDot+KNutritionId+KAs+nutritionId+QString::number(0)+KAs+nutritionQ
            +KFrom+KTableRecipe+KJoin+KTableNutrition
            +KUnion
            +KSelect+KTableRecipeIngredient+KDot+KRecipeIngredientRecipeId+KComma+KTableIngredientNutrition+KDot+KIngredientNutritionNutritionId+KComma
            +KSum+"("+KTableIngredientNutrition+KDot+KIngredientNutritionNutritionQuantity+KOpMultiply
            +KTableRecipeIngredient+KDot+KRecipeIngredientQuantity+KOpDivide
            +KTableIngredientNutrition+KDot+KIngredientNutritionIngredientQuantity;
    if (perServe) {
        sql += KOpDivide+KTableRecipe+KDot+KRecipeServe;
    }
    sql += ")"
            + KFrom+KTableRecipeIngredient+KJoin+KTableIngredientNutrition+KOn
            +KTableRecipeIngredient+KDot+KRecipeIngredientIngredientId+KOpEqual+KTableIngredientNutrition+KDot+KIngredientNutritionIngredientId;
    if (perServe) {
        sql += KJoin+KTableRecipe+KOn+KTableRecipeIngredient+KDot+KRecipeIngredientRecipeId+KOpEqual+KTableRecipe+KDot+KRecipeId;
    }
    sql += KGroupBy+KTableRecipeIngredient+KDot+KRecipeIngredientRecipeId+KComma+KTableIngredientNutrition+KDot+KIngredientNutritionNutritionId
            +")"
            +KGroupBy+recipeId+KComma+nutritionId
            +KHaving;
    for (int i = 0; i < filters.count(); i++) {
        sql += "("
                + sqlNutritionFilterReverseString(filters[i], nutritionId, nutritionSum)
                + ")";
        if (i != filters.count() - 1) {
            sql += KOr;
        }
    }
    sql += QString(")")
           +")";
    return sql;
}

QString CRecipeModel::sqlNutritionFilterReverseString(
    CNutritionFilter& filter, const QString& nutritonIdStr,
    const QString& nutritionSumStr) {
    CNutritionFilter reverse;
    sqlNutritionReverseFilter(filter, reverse);
    return sqlNutritionFilterString(reverse, nutritonIdStr, nutritionSumStr);
}

QString CRecipeModel::sqlNutritionFilterString(
    CNutritionFilter& filter, const QString& nutritonIdStr,
    const QString& nutritionSumStr) {
    QString sql;
    sql += nutritonIdStr+KEqual+QString::number(filter.nutritionId())
            +KAnd+nutritionSumStr;
    switch(filter.moreLess()) {
    case CRecipeFilter::EMore:
        sql += KOpMore;
        break;

    case CRecipeFilter::ELess:
        sql += KOpLess;
        break;

    case CRecipeFilter::EEqual:
        sql += KOpEqual;
        break;

    case CRecipeFilter::ENotEqual:
        sql += KOpNotEqual;
        break;

    case CRecipeFilter::ELessOrEqual:
        sql += KOpLessEqual;
        break;

    case CRecipeFilter::EMoreOrEqual:
        sql += KOpMoreEqual;
        break;

    default:
        break;
    }
    sql += filter.quantity();
    return sql;
}

void CRecipeModel::sqlNutritionReverseFilter(
    CNutritionFilter& filter,
    CNutritionFilter& reverse) {
    reverse = filter;
    switch (filter.moreLess()) {
    case CRecipeFilter::EMore:
        reverse.setMoreLess(CRecipeFilter::ELessOrEqual);
        break;

    case CRecipeFilter::ELess:
        reverse.setMoreLess(CRecipeFilter::EMoreOrEqual);
        break;

    case CRecipeFilter::EEqual:
        reverse.setMoreLess(CRecipeFilter::ENotEqual);
        break;

    case CRecipeFilter::ENotEqual:
        reverse.setMoreLess(CRecipeFilter::EEqual);
        break;

    case CRecipeFilter::ELessOrEqual:
        reverse.setMoreLess(CRecipeFilter::EMore);
        break;

    case CRecipeFilter::EMoreOrEqual:
        reverse.setMoreLess(CRecipeFilter::ELess);
        break;

    default:
        break;
    }
}

bool CRecipeModel::sqlNutritionsHasPerServe() {
    QList<CNutritionFilter> filters = m_filter.nutritions();
    if (filters.count()) {
        return filters[0].recipeFilterType() == CNutritionFilter::EPerServe;
    }
    else {
        return false;
    }
}

void CRecipeModel::sqlAddWhereUnion(QString& sql, const QString& subSql) {
    if (!subSql.isEmpty()) {
        if (!sql.isEmpty()) {
            sql += KUnion+subSql;
        }
        else {
            sql += subSql;
        }
    }
}
